package tools 
{
	import Entities.Note;
	import flash.events.MouseEvent;
	import flash.display.Graphics;
	import Entities.InstrumentLayer;
	import screens.NoteCanvas;
	import flash.geom.Point;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import Buttons.Tool;
	/**
	 * ...
	 * @author Andy Wolff
	 */
	public class DrawNotesTool implements INoteTool
	{
		protected var view:NoteCanvas;
		protected var mouseclicked:Boolean = false;
		protected var drawnlines:Array = null;
		protected var drawnlinespos:int = 0;
		protected var currentNote:Note;
		protected var instrument:InstrumentLayer;
		
		public function DrawNotesTool(view:NoteCanvas) 
		{
			this.view = view;
			instrument = new InstrumentLayer(view, 0x000000,-1);
		}
		
		public function mouseDown(e:MouseEvent):void {
			if (e.localY < 56 || mouseclicked || (drawnlines!=null && drawnlinespos<drawnlines.length) || currentNote!=null) return;
			drawnlinespos = 0;
			mouseclicked = true;
			drawnlines = new Array();
			drawnlines.push(new Point(e.localX, e.localY));
			//trace("mouse down"+"("+e.localX+","+e.localY+")");
		}
		
		public function mouseMove(e:MouseEvent):void {
			if (e.localY < 56) e.localY=56;
			if (mouseclicked && drawnlines!=null) drawnlines.push(new Point(e.localX, e.localY));//trace("mouse move"+"("+e.localX+","+e.localY+")");
		}
		
		public function mouseUp(e:MouseEvent):void {
			if (e.localY < 56) return;
			if (drawnlines == null || !mouseclicked) { mouseclicked = false;  return; }
			drawnlines.push(new Point(e.localX, e.localY));
			//for each (var p:Point in drawnlines) { p.y = Math.min(7, Math.floor((p.y - 56) / 44)) * 44 + 22 + 56; }
			//drawnlines.sortOn(["y","x"],Array.NUMERIC);
			mouseclicked = false;
			//trace("mouse up"+"("+e.localX+","+e.localY+")");
		}
			
		public function end():void {
			mouseclicked = false;
		}
		
		public function draw(graphics:Graphics):void {
			if (view.whichTool() == Tool.DRAW) instrument.color = view.getCurrentInstrument().color;
			else instrument.color = 0x000000;
			instrument.draw(graphics);
			var i:int;
			var viewPos:Number = view.getViewPos();
			var currentInstrument:InstrumentLayer = instrument;//view.getCurrentInstrument();
			graphics.lineStyle(2, 0x220000, 1, false, "normal", CapsStyle.SQUARE, JointStyle.MITER);
			if (drawnlines != null && drawnlines[0]!=null) {
				
				//iteratively snap the points to the note rows
				if (drawnlinespos < drawnlines.length) for (i = 0; i < /*1+*/(drawnlines.length-drawnlinespos)/*/20*/ && drawnlinespos < drawnlines.length; i++) { 
					drawnlines[drawnlinespos].y = NoteCanvas.snapToNoteRow(drawnlines[drawnlinespos].y); 
					drawnlinespos++;
				}
				//draw the points
				graphics.moveTo(drawnlines[0].x, drawnlines[0].y);
				for each (var p:Point in drawnlines) graphics.lineTo(p.x, p.y);
				
				
				if (drawnlinespos >= drawnlines.length && drawnlines.length > 0 && !mouseclicked) {
					//convert the points to notes
					for (i = 0; i < /*1 +*/ drawnlines.length /*/ 20*/ && drawnlines.length > 0; i++) {
						p = drawnlines.shift(); 
						if (currentNote == null) 
							{ 
								//start a note in the row
								currentNote = new Note(currentInstrument, NoteCanvas.getNoteRow(p.y), NoteCanvas.getNoteX(p.x), NoteCanvas.getNoteX(p.x)); currentInstrument.addNote(currentNote); 
							}
						else if (Math.floor((p.y - 56) / 44) != currentNote.getWhich()) 
							{ 
								currentNote.extend(new Point(NoteCanvas.getNoteX(p.x), p.y)); //prevent gaps
								//start a new note in the next row
								currentNote = new Note(currentInstrument, NoteCanvas.getNoteRow(p.y), NoteCanvas.getNoteX(p.x), NoteCanvas.getNoteX(p.x)); currentInstrument.addNote(currentNote); 
							}
						else currentNote.extend(new Point(NoteCanvas.getNoteX(p.x), p.y)); //continue making current note
						
						//if we've finished, end it
						if (drawnlines.length <= 0) {
							finish();
							instrument = new InstrumentLayer(view, 0x000000,-1);
							currentNote = null;
						}
					}
				}
			}
		}
		
		public function finish():void {
			view.saveState();
			if (view.whichTool()==Tool.DRAW) view.getCurrentInstrument().mergeInstrument(instrument);
			else view.getCurrentInstrument().subtractInstrument(instrument);
		}
	}

}